package com.bang.study.day02.test;

import java.util.Stack;

/**
 * @Auther: Bang
 * @Date: 2019/9/12 0012 15:03
 * @Description: 验证二叉搜索树
 */
public class Solution98 {


    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }else{
            int min = Integer.MIN_VALUE;
            TreeNode curr = root;
            boolean flag = false;
            Stack<TreeNode> stack = new Stack<>();
            while (curr!=null||!stack.empty()){
                while (curr != null) {
                    stack.push(curr);
                    curr = curr.left;
                }
                curr = stack.pop();
                int val = curr.val;
                if (flag && val <= min) {
                    return false;
                }
                flag = true;
                min = val;
                curr = curr.right;
            }
            return true;
        }

    }





    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
